﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="GamePlayScreen.cs" company="">
//   
// </copyright>
// <summary>
//   The gameplay screen.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

/*
 * Author: Mikkel Thordal Andersen, Juan Mompean Esteban
 * Created: 26-11-2011
 */
namespace ITU_vs_Zombies.Menu.Screens
{
    using ITU_vs_Zombies.Game;
    using ITU_vs_Zombies.Scenery;

    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.Input;
    using Microsoft.Xna.Framework.Media;

    /// <summary>
    /// The gameplay screen.
    /// </summary>
    public class GameplayScreen : GameScreen
    {
        #region Constants and Fields

        /// <summary>
        /// The content.
        /// </summary>
        private ContentManager content;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="GameplayScreen"/> class.
        /// </summary>
        public GameplayScreen()
        {
            this.SongName = @"Sounds\GameSoundTest";
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// The activate.
        /// </summary>
        /// <param name="instancePreserved">
        /// The instance preserved.
        /// </param>
        public override void Activate(bool instancePreserved)
        {
            if (instancePreserved)
            {
                return;
            }

            if (this.content == null)
            {
                this.content = new ContentManager(this.ScreenManager.Game.Services, "Content");
            }

            Client.SetContentAndDevice(this.content, this.ScreenManager.GraphicsDevice);
            Client.GameState = GameStateEnum.Normal;
            Client.ResetGameTime();
            var floor = new Floor(@"FloorsBackground\floor0", 0, true, 7);
            floor.AddPlayer(Client.MainPlayer);
            floor.Visited = true;
            Client.AddFloor(floor);
            Client.AddFloor(new Floor(@"FloorsBackground\floor1", 1, false, 10));
            Client.AddFloor(new Floor(@"FloorsBackground\floor2", 2, false, 20));
            Client.AddFloor(new Floor(@"FloorsBackground\floor3", 3, false, 27));
            Client.AddFloor(new Floor(@"FloorsBackground\floor4", 4, false, 35));
            Client.AddFloor(new Floor(@"FloorsBackground\floor5", 5, false, 1));
            Client.Difficulty = 5;
            Client.LoadContent();
            this.Song = this.content.Load<Song>(this.SongName);

            this.ScreenManager.Game.ResetElapsedTime();
        }

        /// <summary>
        /// The draw.
        /// </summary>
        /// <param name="gameTime">
        /// The game time.
        /// </param>
        public override void Draw(GameTime gameTime)
        {
            this.ScreenManager.SpriteBatch.Begin();
            Client.Draw(this.ScreenManager.SpriteBatch);
            this.ScreenManager.SpriteBatch.End();
        }

        /// <summary>
        /// The handle input.
        /// </summary>
        /// <param name="gameTime">
        /// The game time.
        /// </param>
        /// <param name="input">
        /// The input.
        /// </param>
        public override void HandleInput(GameTime gameTime, InputState input)
        {
            var escape = new InputAction(new[] { Keys.Escape }, true);
            var sKey = new InputAction(new[] { Keys.S }, true);
            var pKey = new InputAction(new[] { Keys.P }, true);

            if (escape.Evaluate(input))
            {
                this.ExitScreen();
                MediaPlayer.Stop();
                MediaPlayer.MovePrevious();
                Client.StoreScore();
                Client.Dispose();
                Client.MainPlayer = null;
            }

            // If the S key is pushed the player goes to the shop.
            if (sKey.Evaluate(input))
            {
                Client.GameState = GameStateEnum.Paused;
                this.ScreenManager.AddScreen(new ShopScreen());
            }

            // If the P key is pressed the game is paused.
            if (pKey.Evaluate(input))
            {
                switch (Client.GameState)
                {
                    case GameStateEnum.Normal:

                        // Save the gametime!
                        Client.GameTime = gameTime;
                        Client.GameState = GameStateEnum.Paused;
                        this.ScreenManager.AddScreen(new PauseScreen(this));
                        break;
                }
            }
        }

        /// <summary>
        /// The unload.
        /// </summary>
        public override void Unload()
        {
            if (this.content != null)
            {
                this.content.Unload();
            }

            base.Unload();
        }

        /// <summary>
        /// The update.
        /// </summary>
        /// <param name="gameTime">
        /// The game time.
        /// </param>
        /// <param name="otherScreenHasFocus">
        /// The other screen has focus.
        /// </param>
        /// <param name="coveredByOtherScreen">
        /// The covered by other screen.
        /// </param>
        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            if (Client.GameState == GameStateEnum.Normal)
            {
                Client.GameTime = gameTime;
                Client.Update(Client.GameTime);
                base.Update(Client.GameTime, otherScreenHasFocus, false);
            }
            else if (Client.GameState == GameStateEnum.GameOver)
            {
                MediaPlayer.Stop();
                this.ExitScreen();
                this.ScreenManager.AddScreen(new FinalScreen("Game Over", @"Sounds\GameOver"));
                Client.StoreScore();
                Client.Dispose();
            }
            else if (Client.GameState == GameStateEnum.Win)
            {
                MediaPlayer.Stop();
                this.ExitScreen();
                this.ScreenManager.AddScreen(new FinalScreen("You win!!", @"Sounds\GameOver"));
                Client.StoreScore();
                Client.Dispose();
            }
        }

        #endregion
    }
}